InlineUpload.cleanUp   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
var InlineUpload = {
2
    dialog: null,
3
    options: {
4
        form_class: 'inline_upload_form',
5
        action: '/posts/upload',
6
        iframe: 'inline_upload_iframe'
7
    },
8
    display: function(hash) {
0 ignored issues
show
Unused Code introduced by
The parameter hash is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
9
        var self = this;
10
11
        this.dialog = $(document).find(".inline_upload_container");
12
13
        if (!this.dialog.size()) {
14
            // Create invisible form and iframe
15
            this.dialog = $([
16
                '<div style="opacity:0;position:absolute;" class="inline_upload_container"><form class="',this.options.form_class,'" action="',this.options.action,'" target="',this.options.iframe,'" method="post" enctype="multipart/form-data">',
17
                '<input name="upload_file" type="file" /></form>' +
18
                '<iframe id="',this.options.iframe,'" name="',this.options.iframe,'" class="',this.options.iframe,'" src="about:blank" width="0" height="0"></iframe></div>',
19
            ].join(''));
20
            this.dialog.appendTo(document.body);
21
        }
22
23
        // make 'click' action on file element right after 'Picture' selection on markItUp menu
24
        // to show system dialog
25
        $("input[name='upload_file']").focus();
26
        $("input[name='upload_file']").trigger('click');
27
28
        // submit hidden form after file was selected in system dialog
29
        $("input[name='upload_file']").on('change', function(){
30
            if ($(this).val() != '') {
31
                $('.' + self.options.form_class).submit();
32
            }
33
        });
34
35
        // response will be sent to the hidden iframe
36
        $('.' + this.options.iframe).bind('load', function() {
37
            var responseJSONStr = $(this).contents().text();
38
            if (responseJSONStr != '') {
39
                var response = $.parseJSON(responseJSONStr);
40
                if (response.status == 'success') {
41
                    var block = ['<img src="' + response.src + '" width="' + response.width + '" height="' + response.height + '" alt="" class=""/>'];
42
                    $.markItUp({replaceWith: block.join('')} );
43
                } else {
44
                    alert(response.msg);
0 ignored issues
show
Debugging Code Best Practice introduced by
The alert UI element is often considered obtrusive and is generally only used as a temporary measure. Consider replacing it with another UI element.
Loading history...
45
                }
46
                self.cleanUp();
47
            }
48
        });
49
    },
50
    cleanUp: function() {
51
        $("input[name='upload_file']").off('change');
52
        this.dialog.remove();
53
    }
54
};
55